package com.tpyule.tpclass.web.Controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.tpyule.tpclass.entity.bean.FailResultBean;
import com.tpyule.tpclass.entity.bean.ResultBean;
import com.tpyule.tpclass.entity.bean.SuccessResultBean;
import com.tpyule.tpclass.entity.bo.MyQuestionBo;
import com.tpyule.tpclass.entity.bo.QuestionAnswerBo;
import com.tpyule.tpclass.entity.bo.QuestionDetailBo;
import com.tpyule.tpclass.entity.dto.MyQuestionQueryDto;
import com.tpyule.tpclass.entity.dto.QuestionAnswerDto;
import com.tpyule.tpclass.entity.dto.QuestionQueryDto;
import com.tpyule.tpclass.entity.pojo.Course;
import com.tpyule.tpclass.entity.pojo.Question;
import com.tpyule.tpclass.entity.pojo.QuestionFollow;
import com.tpyule.tpclass.service.QuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author zzz
 * @version 1.0
 * @date 2020/4/16 10:16
 */
@RestController
@RequestMapping("/question")
public class QuestionController extends BaseController {
    @Autowired
    QuestionService questionService;
    @RequestMapping("/add")
    @ResponseBody
    public void addQuestion(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ResultBean resultBean=null;
        ObjectMapper objectMapper=new ObjectMapper();
        try {
            String questionStr = request.getParameter("questionData");
            if(questionStr==null||"".equals(questionStr)){
                resultBean = new FailResultBean("数据错误，请重试");
            }else {
                Question question=objectMapper.readValue(questionStr, Question.class);
                resultBean = questionService.addQuestion(question);
            }
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        } finally {
            this.setAjaxResponseResult(response,resultBean);
        }
    }

    @RequestMapping("/getDetail")
    @ResponseBody
    public void getDetail(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ResultBean resultBean=null;
        ObjectMapper objectMapper=new ObjectMapper();
        try {
            String queryData = request.getParameter("queryData");

            if(queryData==null||"".equals(queryData)){
                resultBean = new FailResultBean("数据错误，请重试");
            }else {
                QuestionQueryDto dto = objectMapper.readValue(queryData, QuestionQueryDto.class);
                QuestionDetailBo bo = questionService.getQuestionDetail(dto);
                if (bo!=null)
                    resultBean = new SuccessResultBean(bo, "成功");
                else
                    resultBean = new FailResultBean("失败");
            }
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        } finally {
            this.setAjaxResponseResult(response,resultBean);
        }
    }
    @RequestMapping("/getListQuestion")
    @ResponseBody
    public void getListQuestion(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ResultBean resultBean=null;
        ObjectMapper objectMapper=new ObjectMapper();
        try {
            String queryData = request.getParameter("queryData");

            if(queryData==null||"".equals(queryData)){
                resultBean = new FailResultBean("数据错误，请重试");
            }else {
                QuestionAnswerDto dto = objectMapper.readValue(queryData, QuestionAnswerDto.class);
                List<QuestionAnswerBo> bos = questionService.getListQuestion(dto);
                if (bos!=null)
                    resultBean = new SuccessResultBean(bos, "成功");
                else
                    resultBean = new FailResultBean("失败");
            }
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        } finally {
            this.setAjaxResponseResult(response,resultBean);
        }
    }

    @RequestMapping("/getMyQuestion")
    @ResponseBody
    public void getMyQuestion(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ResultBean resultBean=null;
        ObjectMapper objectMapper=new ObjectMapper();
        try {
            String queryData = request.getParameter("queryData");

            if(queryData==null||"".equals(queryData)){
                resultBean = new FailResultBean("数据错误，请重试");
            }else {
                MyQuestionQueryDto dto = objectMapper.readValue(queryData, MyQuestionQueryDto.class);
                List<MyQuestionBo> bos = questionService.getListMyQuestion(dto);
                if (bos!=null)
                    resultBean = new SuccessResultBean(bos, "成功");
                else
                    resultBean = new FailResultBean("失败");
            }
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        } finally {
            this.setAjaxResponseResult(response,resultBean);
        }
    }

    @RequestMapping("/addFollow")
    @ResponseBody
    public void addFollow(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ResultBean resultBean=null;
        ObjectMapper objectMapper=new ObjectMapper();
        try {
            String followData = request.getParameter("followData");

            if(followData==null||"".equals(followData)){
                resultBean = new FailResultBean("数据错误，请重试");
            }else {
                QuestionFollow questionFollow = objectMapper.readValue(followData, QuestionFollow.class);
                int result = questionService.addFollow(questionFollow);
                if (result!=0)
                    resultBean = new SuccessResultBean( "成功");
                else
                    resultBean = new FailResultBean("失败");
            }
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        } finally {
            this.setAjaxResponseResult(response,resultBean);
        }
    }

    @RequestMapping("/cancleFollow")
    @ResponseBody
    public void cancleFollow(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ResultBean resultBean=null;
        ObjectMapper objectMapper=new ObjectMapper();
        try {
            String id = request.getParameter("id");

            if(id==null||"".equals(id)){
                resultBean = new FailResultBean("数据错误，请重试");
            }else {
                int result = questionService.updateFollow(id);
                if (result!=0)
                    resultBean = new SuccessResultBean( "成功");
                else
                    resultBean = new FailResultBean("失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            this.setAjaxResponseResult(response,resultBean);
        }
    }

    @RequestMapping("/getFollowQuestion")
    @ResponseBody
    public void getFollowQuestion(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ResultBean resultBean=null;
        ObjectMapper objectMapper=new ObjectMapper();
        try {
            String queryData = request.getParameter("queryData");

            if(queryData==null||"".equals(queryData)){
                resultBean = new FailResultBean("数据错误，请重试");
            }else {
                MyQuestionQueryDto dto = objectMapper.readValue(queryData, MyQuestionQueryDto.class);
                List<MyQuestionBo> bos = questionService.getFollowQuestion(dto);
                if (bos!=null)
                    resultBean = new SuccessResultBean(bos, "成功");
                else
                    resultBean = new FailResultBean("失败");
            }
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        } finally {
            this.setAjaxResponseResult(response,resultBean);
        }
    }

}
